package com.genesis.yunnanji.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.genesis.yunnanji.MainActivity;
import com.genesis.yunnanji.R;


public class WelcomeFragment extends Fragment implements View.OnClickListener {

    private int index;
    private ImageView iv;
    private Button btn_go;
    private View view_welcome;

    public static WelcomeFragment newInstance(int param) {
        WelcomeFragment obj = new WelcomeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view_welcome == null) {
            view_welcome = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_welcome, null);
            index = getArguments().getInt("args1");
            initView(view_welcome);
            setUpView();
        } else {
            ViewGroup parent = (ViewGroup) view_welcome.getParent();
            if (parent != null) {
                parent.removeView(view_welcome);
            }
        }
        return view_welcome;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_welcome:
                startActivity(new Intent(getContext(), MainActivity.class));
                getActivity().finish();
                break;
        }
    }

    protected void initView(View view) {
        view_welcome = view;
        iv = (ImageView) view.findViewById(R.id.iv_welcome);
        btn_go = (Button) view.findViewById(R.id.btn_welcome_go);
        btn_go.setOnClickListener(this);

    }

    protected void setUpView() {
        switch (index) {
            case 1:
                iv.setImageResource(R.mipmap.showone);
                break;
            case 2:
                iv.setImageResource(R.mipmap.showtwo);
                break;
            case 3:
                iv.setImageResource(R.mipmap.showthree);
                iv.setOnClickListener(this);
                break;

        }
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
}
